package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.Arith;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.OpportunityProductDao;
import com.xbongbong.saas.domain.entity.OpportunityProductEntity;
import com.xbongbong.saas.model.OpportunityProductModel;
import org.apache.commons.collections4.CollectionUtils;
import com.xbongbong.saas.util.StandardizeDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/2/23 14:29
 * @since v1.0
 */
@Service("opportunityProductModel")
public class OpportunityProductModelImpl implements OpportunityProductModel {
    @Resource
    private OpportunityProductDao opportunityProductDao;

    @Override
    public Integer insert(OpportunityProductEntity entity) throws XbbException {
        StandardizeDataUtil.standardizeNotString(entity);
        return opportunityProductDao.insert(entity);
    }

    @Override
    public Integer insertBatch(List<OpportunityProductEntity> insertList) throws XbbException {
        if (CollectionUtils.isEmpty(insertList)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        insertList.forEach(item->{
            item.setAddTime(now);
            item.setUpdateTime(now);
        });
        return opportunityProductDao.insertBatch(insertList);
    }

    @Override
    public Integer update(OpportunityProductEntity entity) throws XbbException {
        entity.setUpdateTime(DateUtil.getNow());
        StandardizeDataUtil.standardizeNotString(entity);
        return opportunityProductDao.update(entity);
    }

    @Override
    public List<OpportunityProductEntity> findEntitys(Map<String, Object> param) {
        return opportunityProductDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return opportunityProductDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer save(OpportunityProductEntity entity) throws XbbException {
        Long id = entity.getId();
        if (id == null || Objects.equals(id, 0L)) {
            return insert(entity);
        } else {
            return update(entity);
        }
    }

    @Override
    public List<OpportunityProductEntity> getProductsByOppId(Long oppId, String corpid, List<Integer> delIn) {
        // 获取机会关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("delIn", delIn);
        params.put("opportunityId", oppId);
        params.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        return findEntitys(params);
    }

    @Override
    public List<OpportunityProductEntity> getProductsByOppId(Long oppId, String corpid) {
        // 获取机会关联产品
        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("corpid", corpid);
        params.put("del", 0);
        params.put("opportunityId", oppId);
        params.put(ParameterConstant.ORDER_BY_STR, StringConstant.SAAS_LINK_BUSINESS_ID);
        return findEntitys(params);
    }

    @Override
    public Double getEstimateCost(String corpid, Long opportunityId) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("opportunityId", opportunityId);
        param.put("corpid", corpid);
        param.put("del", 0);
        List<OpportunityProductEntity> opportunityProductList = this.findEntitys(param);

        // 判断是否为空
        if (opportunityProductList.isEmpty()) {
            return 0D;
        }

        // 机会预估成本=SUM（产品数量*产品成本）
        Double estimageCost = 0D;
        for (OpportunityProductEntity opportunityProductEntity : opportunityProductList) {
            estimageCost = Arith.add(estimageCost, Arith.mul(opportunityProductEntity.getProductNum(), opportunityProductEntity.getCost()));
        }
        return estimageCost;
    }

    @Override
    public Integer dataConsistencyUpdate(String corpid, Long productId, String productName, String specification, String productNo) {
        return opportunityProductDao.dataConsistencyUpdate(corpid, productId, productName, specification, productNo);
    }

    @Override
    public Long isProductSubAttrExist(String attr, String corpid, Long formId, Integer isArray) {
        return opportunityProductDao.isProductSubAttrExist(attr, corpid, formId, isArray);
    }

    @Override
    public Integer deleteBatchByProductId(List<Long> productIdList, String corpid) {
        return opportunityProductDao.deleteBatchByProductId(productIdList, corpid);
    }

    @Override
    public Double summaryByAttr(String field,String corpid,  Long refId, List<Long> productIdIn) {
        return opportunityProductDao.summaryByAttr( field,corpid, refId, productIdIn);
    }

    @Override
    public Double summaryByAttrFromOutSide(String field, String corpid, Long refId, List<Long> productIdIn) {
        return opportunityProductDao.summaryByAttrFromOutSide( field,corpid, refId, productIdIn);
    }

    @Override
    public Long countProductSub(String attr, String corpid, Long formId, Integer isArray) {
        return opportunityProductDao.countProductSub(attr, corpid, formId, isArray);
    }

    @Override
    public List<OpportunityProductEntity> getProductWithSubData(String attr, String corpid, Long formId, Integer isArray, Long idGte, Integer limit) {
        return opportunityProductDao.getProductWithSubData(attr, corpid, formId, isArray, idGte, limit);
    }

    @Override
    public Integer deleteBatch(List<Long> deleteList, String corpid, Integer del) {
        if (CollectionUtils.isEmpty(deleteList)) {
            return 0;
        }
        return opportunityProductDao.deleteBatch(deleteList, corpid, del);
    }

    @Override
    public Integer updateMulti(List<OpportunityProductEntity> list, String corpid) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        long now = DateTimeUtil.getInt();
        list.forEach(item->{
            item.setUpdateTime(now);
        });
        StandardizeDataUtil.standardize4OpportunityProductEntityList(list);
        return opportunityProductDao.updateMulti(list, corpid);
    }
}
